package hibu.LeiJonGun.customViews;

import hibu.LeiJonGun.activities.ActViewAssignment;
import hibu.LeiJonGun.seniorservice.R;
import hibu.LeiJonGun.seniorservice.sizeType;
import android.content.Context;
import android.util.TypedValue;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

/*
 * Written by: Christian L. Gunvaldsen
 * 
 * This classed is being used by 
 * ActViewAssignment and represent an assignment
 * 
 * It has an image that shows a "check mark" or hourglass depending 
 * on the status of the assignment
 */
public class CustomViewAssignment extends CustomAbstractViewGroup implements OnClickListener {
	private TextView lblName;
	private int assignmentID;
	private ImageView imgStatus;
	private String status;
	private final String taken = "taken";
	private final String finished = "finished";
	private int posCheckLeft;
	private int posGlassLeft;
	
// the constructor
	
	public CustomViewAssignment(Context context) {// not used
		super(context);
	}
	
	public CustomViewAssignment(Context context, int viewIdx, String text, 
			int assignmentID, String status, sizeType screenSize) throws ClassCastException { // this one is being used
		super(context);
		if (!context.getClass().equals(ActViewAssignment.class)) throw new ClassCastException("Wrong context: "+context.getClass()); // to make sure its the right context
		this.status = status;
		this.assignmentID=assignmentID;
		this.screenSize=screenSize;
		getScreenSize(); // check screen size and get sizes for the view + text
		setSizes();
		initView(viewIdx,text);
	}
	
	private void initView(int viewIdx,String text) { // custom for this textview
		//Set background:
		this.setBackgroundResource(R.drawable.lbl_find_assignment_selector);
		
		//Listener:
		this.setOnClickListener(this); // is it own listener
		
		lblName = new TextView(this.getContext());
		lblName.setText(text);
		lblName.setTextColor(this.getResources().getColor(R.color.lblText));
		lblName.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
		lblName.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
		
		// Show image if assignment is taken or finished:
		if (status.equals(finished)) {
			imgStatus = new ImageView(this.getContext());
			imgStatus.setBackgroundResource(R.drawable.symbol_check);
			imgStatus.setTag(finished);
		}
		if (status.equals(taken)) {
			imgStatus = new ImageView(this.getContext());
			imgStatus.setBackgroundResource(R.drawable.hourglass);
			imgStatus.setTag(taken);
		}
		
	    //set position, size and text size
  		RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,marginTopPx); // width and height of the view
  		rlp.setMargins(0, marginTopPx * viewIdx, 0, 0); // (marginTopPx * viewIdx) if marginTopPx is 100 and the viewIdx is 1, the view will have a marginTop of 100
  		this.setLayoutParams(rlp);
  		setPadding();
  		
  		//add children views
  		this.addView(lblName);
  		if (status.equals(taken)) this.addView(imgStatus);
  		if (status.equals(finished)) this.addView(imgStatus);
	}
	
	private void setSizes() { // position images by screen sizes
		switch (screenSize) {
		case Small:
			posCheckLeft = (int)this.getResources().getDimension(R.dimen.assignmentCheckPosLeftSmall);
			posGlassLeft = (int)this.getResources().getDimension(R.dimen.assignmentGlassPosLeftSmall);
			break;
		case Medium:
			posCheckLeft = (int)this.getResources().getDimension(R.dimen.assignmentCheckPosLeftMedium);
			posGlassLeft = (int)this.getResources().getDimension(R.dimen.assignmentGlassPosLeftMedium);
			break;
		case Large:
			posCheckLeft = (int)this.getResources().getDimension(R.dimen.assignmentCheckPosLeftLarge);
			posGlassLeft = (int)this.getResources().getDimension(R.dimen.assignmentGlassPosLeftLarge);
			break;
		case XLarge:
			posCheckLeft = (int)this.getResources().getDimension(R.dimen.assignmentCheckPosLeftXLarge);
			posGlassLeft = (int)this.getResources().getDimension(R.dimen.assignmentGlassPosLeftXLarge);
			break;
		}
		
	}
	
	
	@Override
	protected void onLayout(boolean bool, int l, int t, int r, int b) { // position child views
		for (int i =0; i<this.getChildCount();i++) {
			View Child = this.getChildAt(i);
			if (Child.getClass().equals(TextView.class)) {t =padding;l = padding;} // user name
			if (Child.getClass().equals(ImageView.class)) { //image:
				t=padding;
				r= viewWidth-padding;
				if (Child.getTag().equals(finished)) l = viewWidth-posCheckLeft;
				else if (Child.getTag().equals(taken)) l = viewWidth-posGlassLeft;
				b= viewHeight-padding;
			}
			Child.layout(l, t, r, b);	
		}
		
	}

	public void onClick(View v) {
		((ActViewAssignment)this.getContext()).assignmentClicked(assignmentID); //Update activity
	}
	
	
}